﻿package com.sour
{
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
	import flash.external.ExternalInterface;	
	
	public class host extends Sprite
	{
		private var _keaurl:String; 
		private var _keatree:XML;
		private var _rootnode:XML;
		private var _id:String;
		private var _fatherid:String;
		private var _drawlimit:int;
		private var _maxdis:Number;
		private var _rootx:Number;
		private var _rooty:Number;
		private var _nodename;		
		private var _proxy;
		private var _back;
		
		public function host(a:String,id:String)
		{
			KEALog("KEA Demo");
			_keaurl = a;
			_id = id;
			_drawlimit = 3;
			_maxdis = 80;
			_rootx = 150;
			_rooty = 150;
			_proxy = new Sprite();
			addChild(_proxy);
			_back = new up();
			addChild(_back);
			_back.x = 30;
			_back.y = 30;
			_back.visible = false;
			_back.addEventListener(MouseEvent.CLICK,handleBack);
			loadKEAData();
		}
		
		public function handleBack(event:MouseEvent):void
		{
			if(ExternalInterface.available)
			{
				ExternalInterface.call("redirect",_fatherid);
			}		
		}		
		
		private function loadKEAData():void
		{
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,handleComplete);
            loader.addEventListener(IOErrorEvent.IO_ERROR,handleError);	
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,handleError);		
			loader.load(new URLRequest(_keaurl));
		}
		
		private function handleComplete(event:Event):void
		{
			KEALog("Get XML Data Success :-)");
			try
			{
				_keatree = new XML(event.target.data);
				drawKEATree();
			}
			catch(e:Error)
			{
				KEALog("Parse XML Data Fail :-(");			
				KEALog(e);				
			}
		}
		
		private function handleError(event:Event):void
		{
			KEALog("Get XML Data Fail :-(");			
		}
		
		private function drawKEATree():void
		{
			var center:XML = getNodeByID(_keatree,_id);
			var father = center.parent();
			if(father != undefined)
			{
				_fatherid = father.@id;
			}
			
			if(center.@id != "_0")
			{
				_back.visible = true;				
			}
			drawKEANode(center,_rootx,_rooty,_drawlimit);
			_nodename = new nodeinfo();
			_proxy.addChild(_nodename);
			_nodename.visible = false;
		}
		
		private function getNodeByID(node:XML,id:String):XML
		{
			var currentid:String = node.@id;
			if(currentid == id)
			{
				return node;
			}
			else
			{
				var childs:XMLList = node.elements();
				for each(var subnode:XML in childs)
				{
					var child:XML = getNodeByID(subnode,id);
					if(child != null)
					{
						return child;
					}
				}
				return null;
			}
		}
		
		private function drawKEANode(node:XML,nodex:Number,nodey:Number,limit:int):void
		{
			KEALog("KEA Node | name:" + node.@name + " id:" + node.@id + " status:" + node.@status);
			if(limit > 0)
			{	
				var childs:XMLList = node.elements();
				var i:Number = 0;
				for each(var subnode:XML in childs)
				{
					i++;
					var _dis:Number = _maxdis * Math.pow(1.6,limit - _drawlimit);
					var _arg:Number = i / childs.length() * 2.0 * Math.PI + limit * Math.PI / 6;
					var cnodex = nodex + _dis * Math.cos(_arg);
					var cnodey = nodey + _dis * Math.sin(_arg);
            
            		if(limit > 1)
					{
						var kline:Shape = new Shape();
            			kline.graphics.beginFill(0x000000);
            			kline.graphics.lineStyle(2, 0x000000, 100);
						
						var dx = 0;
						var dy = 0;
						if(limit == 2){
							dx = 10 * (cnodex - nodex) / Math.pow((Math.pow((cnodex - nodex),2) + Math.pow((cnodey - nodey),2)),0.5);
							dy = 10 * (cnodey - nodey) / Math.pow((Math.pow((cnodex - nodex),2) + Math.pow((cnodey - nodey),2)),0.5);
							kline.graphics.moveTo(nodex + 1.8 * dx,nodey + 1.8 * dy);
            				kline.graphics.lineTo(cnodex - 1.4 *  dx,cnodey - 1.4 * dy);	
						} else {
							dx = 18 * (cnodex - nodex) / Math.pow((Math.pow((cnodex - nodex),2) + Math.pow((cnodey - nodey),2)),0.5);
							dy = 18 * (cnodey - nodey) / Math.pow((Math.pow((cnodex - nodex),2) + Math.pow((cnodey - nodey),2)),0.5);
							kline.graphics.moveTo(nodex + 1.2 * dx,nodey + 1.2 * dy);
            				kline.graphics.lineTo(cnodex - dx,cnodey - dy);	
						}
            			
						kline.graphics.endFill();
            			_proxy.addChild(kline);
						kline.alpha = 1;
					}
					
					drawKEANode(subnode,cnodex,cnodey,limit - 1);
				}
				
				var anode = new knode(this,node,limit);
				_proxy.addChild(anode);
				anode.x = nodex;
				anode.y = nodey;					
			}
		}
		
		private function KEALog(a:Object):void
		{
			trace(a);			
		}
		
		public function MouseOn(a:knode):void
		{
			_nodename.x = a.x;
			_nodename.y = a.y;		
			_nodename.keainfo.text = a.keanode.@name;
			_nodename.visible = true;
		}
		
		public function MouseOut(a:knode):void
		{
			_nodename.visible = false;
		}		
		
		public function Reload(a:knode):void
		{
			removeChild(_proxy);
			_proxy = new Sprite();
			addChild(_proxy);
			drawKEANode(a.keanode,_rootx,_rooty,_drawlimit);
			_nodename = new nodeinfo();
			_proxy.addChild(_nodename);
			_nodename.visible = false;			
		}
	}
}
